package com.rs.controller.Rs;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyVetoException;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.rs.model.CommonResult;
import com.rs.model.Notice;
import com.rs.service.rs.NoticeService;
import com.rs.util.ImagePanel;
import com.rs.util.SpringContextUtils;

public class NoticeJFame extends JInternalFrame {

	private static final long serialVersionUID = 1L;

	public NoticeJFame() {
		//设置是否允许自由调整大小
		setResizable(true);

		//设置是否提供“最大化”按钮
		setMaximizable(true);

		//设置是否提供“最小化”按钮
		setIconifiable(true);

		//设置是否提供关闭按钮
		setClosable(true);
		getContentPane().setLayout(null);

		setVisible(true);
	}

	public void alert(String alert) {
		JOptionPane.showMessageDialog(this, alert);

	}

	/*************发布通知**********/

	class AddNotice extends NoticeJFame {
		private static final long serialVersionUID = 1L;

		private NoticeService ns = SpringContextUtils.getBean(NoticeService.class);

		private JPanel contentPane;

		private JTextField text_name;//通知名称

		private JTextArea text_reason;//通知内容

		public AddNotice() {
			super();

			//设置标题
			setTitle("发布通知");
			//设置是否允许自由调整大小
			setResizable(false);
			//设置最大化
			setMaximizable(false);
			//设置大小
			setBounds(250, 100, 400, 305);
			//主体
			contentPane = new ImagePanel(400, 305, "twwe2.jpg");
			contentPane.setLayout(null);
			setContentPane(contentPane);

			//创建1个字体实例
			Font font = new Font("宋体", Font.BOLD, 15);

			//名称	
			JLabel name = new JLabel("主 题：");
			name.setFont(font);
			name.setBounds(110, 25, 80, 25);
			text_name = new JTextField();
			text_name.setBounds(170, 25, 120, 25);
			text_name.setColumns(10);
			contentPane.add(name);
			contentPane.add(text_name);

			//内容
			JLabel reason = new JLabel("内 容：");
			reason.setFont(font);
			reason.setBounds(110, 50, 80, 25);
			text_reason = new JTextArea();
			text_reason.setBounds(110, 75, 180, 110);
			text_reason.setColumns(10);
			text_reason.setLineWrap(true);
			contentPane.add(reason);
			contentPane.add(text_reason);

			//确认按钮
			JButton yesButton = new JButton("确认");
			yesButton.setFont(font);
			yesButton.setBounds(110, 200, 70, 25);
			contentPane.add(yesButton);
			//监听
			yesButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					//获取值
					Notice notice = new Notice();
					notice.setNo_name(text_name.getText());
					notice.setNo_reason(text_reason.getText());
					int count = ns.noticAdd(notice);
					if (count != 0) {
						alert("发布成功");
						reset();
					} else {
						alert("发布失败");
					}
				}
			});

			//重置按钮
			JButton resetButton = new JButton("重置");
			resetButton.setFont(font);
			resetButton.setBounds(215, 200, 70, 25);
			contentPane.add(resetButton);
			//监听
			resetButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					reset();

				}
			});

		}

		//重置
		public void reset() {
			//通知名称
			text_name.setText("");
			//通知内容
			text_reason.setText("");

		}

	}

	/**************查询通知**************/
	class QueryNotice extends NoticeJFame {

		private static final long serialVersionUID = 1L;

		private NoticeService ns = SpringContextUtils.getBean(NoticeService.class);

		private Notice notice = new Notice();

		private JTextField noname;//通知名称

		private JPanel contentPane;

		private JTable table;

		// 表头（列名）
		private Object[] columnNames = { "ID", "通知主题", "通知内容", "通知时间", "操作", "操作", "操作" };

		private DefaultTableModel dtable;

		private JLabel show;//显示分页

		private Integer pageNum;//当前页

		private Integer pages;//总页数

		public QueryNotice(final JDesktopPane q) {
			super();
			//设置标题
			setTitle("查询通知");
			//设置是否允许自由调整大小
			setResizable(false);
			//设置最大化
			setMaximizable(false);
			//设置大小
			setBounds(40, 35, 800, 550);
			//主体
			contentPane = new ImagePanel(800, 550, "twwe2.jpg");
			contentPane.setLayout(null);
			setContentPane(contentPane);

			//创建1个字体实例
			Font font = new Font("宋体", Font.BOLD, 14);
			//初始化当前页数为
			notice.setPageNum(1);
			//查询所有
			CommonResult<Map<String, Object>> notices = ns.noticeAllPC(notice);
			//为当前页赋值
			pageNum = (Integer) notices.getData().get("pageNum");
			//总页数
			pages = (Integer) notices.getData().get("pages");
			// 创建一个表格，指定 所有行数据 和 表头
			dtable = new DefaultTableModel(getObjectArys(notices), columnNames);

			table = new JTable(dtable) {
				private static final long serialVersionUID = 1L;

				//设置表格为不可编辑
				@Override
				public boolean isCellEditable(int row, int column) {
					return false;
				}
			};
			//设置行高
			table.setRowHeight(25);
			table.setFont(font);
			table.getTableHeader().setBounds(20, 90, 750, 25);
			table.setBounds(20, 120, 750, 300);
			//表头不可拖动
			table.getTableHeader().setReorderingAllowed(false);
			//设置是否可以选择此模型中的行。 
			table.setRowSelectionAllowed(true);
			//设置表格为不可编辑
			//table.isCellEditable(uList.size(), 6);
			//设置只能选择一行
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

			// 把 表头 添加到容器顶部（使用普通的中间容器添加表格时，表头 和 内容 需要分开添加）
			contentPane.add(table.getTableHeader(), null);
			// 把 表格内容 添加到容器中心
			contentPane.add(table, null);

			//表格的点击事件

			table.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						boolean flag = true;
						// 处理鼠标双击
						//所选的行数
						int row = table.getSelectedRow();
						//所选的列数
						int column = table.columnAtPoint(e.getPoint());
						String pId = table.getValueAt(row, 0).toString(); //得到所在行的第一个列的值，作为下面事件传递的参数

						//得到单元格的内容
						Object valueAt = table.getValueAt(row, column);
						switch ((String) valueAt) {
						case "修改":

							q.add(new NoticeJFame().new UpdateNotice(Integer.valueOf(pId), q));
							flag = true;
							break;
						case "删除":
							int i = ns.noticeDeletePC(Integer.valueOf(pId));
							if (i != 0) {
								alert("删除成功");
							} else {
								alert("删除失败");
							}
							paging("首  页");
							flag = false;
							break;
						default:
							q.add(new NoticeJFame().new DetailsNotice(pId, q));
							break;
						}
						try {
							//使窗口图标化
							setIcon(flag);
						} catch (PropertyVetoException e1) {

							e1.printStackTrace();
						}
					}
				}
			});

			show = new JLabel();
			show.setText("当前页：" + pageNum + " / 总页数：" + pages);
			show.setFont(new Font("宋体", Font.BOLD, 15));
			show.setBounds(300, 420, 200, 30);
			contentPane.add(show, null);

			final JButton fore = new JButton("首  页");
			fore.setBounds(100, 475, 80, 28);
			fore.setFont(font);
			contentPane.add(fore, null);
			fore.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = fore.getText();
					paging(type);

				}
			});

			final JButton previous = new JButton("上一页");
			previous.setBounds(280, 475, 80, 28);
			previous.setFont(font);
			contentPane.add(previous, null);
			previous.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = previous.getText();
					paging(type);
				}
			});

			final JButton later = new JButton("下一页");
			later.setBounds(470, 475, 80, 28);
			later.setFont(font);
			contentPane.add(later, null);
			later.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = later.getText();
					paging(type);

				}
			});

			final JButton aft = new JButton("尾  页");
			aft.setBounds(630, 475, 80, 28);
			aft.setFont(font);
			contentPane.add(aft, null);
			aft.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String type = aft.getText();
					paging(type);

				}
			});

			noname = new JTextField("主题", 10);
			noname.setBounds(130, 10, 90, 20);
			noname.setFont(font);
			contentPane.add(noname, null);

			//查询按钮
			JButton query = new JButton("查询");
			query.setBounds(590, 10, 70, 20);
			query.setFont(font);
			contentPane.add(query);
			query.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					//z主题
					if (!noname.getText().equals("主题")) {

						notice.setNo_name(noname.getText());
					}

					//查询
					//设置当前页为1
					notice.setPageNum(1);
					CommonResult<Map<String, Object>> result = ns.noticeAllPC(notice);
					Object[][] shouye = getObjectArys(result);
					//使用数组 dataVector的值替换 dataVector实例变量中的值。 
					dtable.setDataVector(shouye, columnNames);
					table.setModel(dtable);
					pageNum = (Integer) result.getData().get("pageNum");
					pages = (Integer) result.getData().get("pages");
					show.setText("当前页：" + pageNum + " / 总页数：" + pages);

				}
			});

			//重置按钮
			JButton reset = new JButton("重置");
			reset.setBounds(680, 10, 70, 20);
			reset.setFont(font);
			contentPane.add(reset);
			reset.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					//重置
					noname.setText("主题");
					notice.setNo_name(null);
				}
			});

			//刷新
			JButton refresh = new JButton("刷新");
			refresh.setBounds(620, 40, 70, 20);
			refresh.setFont(font);
			contentPane.add(refresh);
			refresh.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					//查询
					//设置当前页为1
					notice.setPageNum(1);
					CommonResult<Map<String, Object>> result = ns.noticeAllPC(notice);
					Object[][] shouye = getObjectArys(result);
					//使用数组 dataVector的值替换 dataVector实例变量中的值。 
					dtable.setDataVector(shouye, columnNames);
					table.setModel(dtable);
					pageNum = (Integer) result.getData().get("pageNum");
					pages = (Integer) result.getData().get("pages");
					show.setText("当前页：" + pageNum + " / 总页数：" + pages);
				}
			});
		}

		public void paging(String type) {
			switch (type) {
			case "首  页":
				notice.setPageNum(1);
				CommonResult<Map<String, Object>> ufore = ns.noticeAllPC(notice);
				Object[][] shouye = getObjectArys(ufore);
				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(shouye, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) ufore.getData().get("pageNum");
				pages = (Integer) ufore.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);
				break;
			case "上一页":
				//当前页减1
				int previousNum = pageNum - 1;
				if (previousNum < 1) {
					alert("当前已是首页");
					previousNum = 1;
				}
				notice.setPageNum(previousNum);
				CommonResult<Map<String, Object>> upreviou = ns.noticeAllPC(notice);
				Object[][] shangyi = getObjectArys(upreviou);

				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(shangyi, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) upreviou.getData().get("pageNum");
				pages = (Integer) upreviou.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);

				break;
			case "下一页":
				//当前页加1
				int laterNum = pageNum + 1;

				if (laterNum > pages) {
					alert("当前已是尾页");
					laterNum = pages;
				}

				notice.setPageNum(laterNum);
				CommonResult<Map<String, Object>> ulater = ns.noticeAllPC(notice);

				Object[][] xiayiye = getObjectArys(ulater);

				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(xiayiye, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) ulater.getData().get("pageNum");
				pages = (Integer) ulater.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);
				break;
			case "尾  页":
				notice.setPageNum(pages);
				CommonResult<Map<String, Object>> uaft = ns.noticeAllPC(notice);
				Object[][] weiye = getObjectArys(uaft);
				//使用数组 dataVector的值替换 dataVector实例变量中的值。 
				dtable.setDataVector(weiye, columnNames);
				table.setModel(dtable);
				pageNum = (Integer) uaft.getData().get("pageNum");
				pages = (Integer) uaft.getData().get("pages");
				show.setText("当前页：" + pageNum + " / 总页数：" + pages);
				break;
			}
		}

		public Object[][] getObjectArys(CommonResult<Map<String, Object>> notices) {
			@SuppressWarnings("unchecked")
			List<Notice> pList = (List<Notice>) notices.getData().get("list");

			Object[][] rowData = new Object[pList.size()][];
			for (int i = 0, n = pList.size(); i < n; i++) {
				Notice notice = pList.get(i);
				rowData[i] = notice.toObjects(columnNames.length);
			}
			return rowData;
		}

	}

	/******修改*******/
	class UpdateNotice extends NoticeJFame {
		private static final long serialVersionUID = 1L;

		private NoticeService ns = SpringContextUtils.getBean(NoticeService.class);

		private JPanel contentPane;

		private JTextField text_name;//通知名称

		private JTextArea text_reason;//通知内容

		public UpdateNotice(final Integer n_id, JDesktopPane q) {
			super();
			//使窗体只有一个界面
			JInternalFrame[] jif = q.getAllFrames();
			if (jif.length >= 2) {
				jif[0].dispose();
			}
			//设置标题
			setTitle("修改通知");
			//设置是否允许自由调整大小
			setResizable(false);
			//设置最大化
			setMaximizable(false);
			//设置大小
			setBounds(250, 100, 400, 305);
			//主体
			contentPane = new ImagePanel(400, 305, "twwe2.jpg");
			contentPane.setLayout(null);
			setContentPane(contentPane);

			//创建1个字体实例
			Font font = new Font("宋体", Font.BOLD, 15);

			//查询通知
			Notice notice = ns.noticeByTjPC(n_id);
			//名称	
			JLabel name = new JLabel("主 题：");
			name.setFont(font);
			name.setBounds(110, 25, 80, 25);
			text_name = new JTextField(notice.getNo_name());
			text_name.setBounds(170, 25, 120, 25);
			text_name.setColumns(10);
			contentPane.add(name);
			contentPane.add(text_name);

			//内容
			JLabel reason = new JLabel("内 容：");
			reason.setFont(font);
			reason.setBounds(110, 50, 80, 25);
			text_reason = new JTextArea(notice.getNo_reason());
			text_reason.setBounds(110, 75, 180, 110);
			text_reason.setColumns(10);
			text_reason.setLineWrap(true);
			contentPane.add(reason);
			contentPane.add(text_reason);

			//确认按钮
			JButton yesButton = new JButton("确认");
			yesButton.setFont(font);
			yesButton.setBounds(110, 200, 70, 25);
			contentPane.add(yesButton);
			//监听
			yesButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					//获取值
					Notice notice = new Notice();
					notice.setNo_id(n_id);
					notice.setNo_name(text_name.getText());
					notice.setNo_reason(text_reason.getText());
					int count = ns.noticeUpdatePC(notice);
					if (count != 0) {
						alert("修改成功");
						dispose();

					} else {
						alert("修改失败");
					}
				}
			});

			//重置按钮
			JButton resetButton = new JButton("重置");
			resetButton.setFont(font);
			resetButton.setBounds(215, 200, 70, 25);
			contentPane.add(resetButton);
			//监听
			resetButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					reset();

				}
			});

		}

		//重置
		public void reset() {
			//通知名称
			text_name.setText("");
			//通知内容
			text_reason.setText("");

		}

	}

	/*********查看详情****************/
	class DetailsNotice extends NoticeJFame {
		private static final long serialVersionUID = 1L;

		private NoticeService ns = SpringContextUtils.getBean(NoticeService.class);

		private JPanel contentPane;

		private JTextField text_id;//id

		private JTextField text_name;//通知主题

		private JTextArea text_reason;//通知内容

		private JTextField text_time;//发布时间

		public DetailsNotice(String pid, JDesktopPane q) {
			super();
			//使窗体只有一个界面
			JInternalFrame[] jif = q.getAllFrames();
			if (jif.length >= 2) {
				jif[0].dispose();
			}
			//设置标题
			setTitle("通知详情");
			//设置是否允许自由调整大小
			setResizable(false);
			//设置最大化
			setMaximizable(false);
			//设置大小
			setBounds(150, 100, 580, 400);

			contentPane = new ImagePanel(580, 400, "twwe2.jpg");
			contentPane.setLayout(null);
			setContentPane(contentPane);

			//查询
			Notice notice = ns.noticeByTjPC(Integer.valueOf(pid));

			//初始化字体
			Font font = new Font("宋体", Font.BOLD, 15);

			//项目名称:
			JLabel name = new JLabel("通知主题:");
			name.setFont(font);
			name.setBounds(50, 30, 75, 20);
			text_name = new JTextField(notice.getNo_name());
			text_name.setFont(font);
			text_name.setBounds(130, 30, 80, 20);
			//禁止编辑
			text_name.setEditable(false);
			contentPane.add(name);
			contentPane.add(text_name);

			//项目ID
			JLabel id = new JLabel("通知ID:");
			id.setFont(font);
			id.setBounds(50, 60, 75, 20);
			text_id = new JTextField(String.valueOf(notice.getNo_id()));
			text_id.setFont(font);
			text_id.setBounds(130, 60, 80, 20);
			//禁止编辑
			text_id.setEditable(false);
			contentPane.add(id);
			contentPane.add(text_id);

			//项目简介
			JLabel reason = new JLabel("通知内容:");
			reason.setFont(font);
			reason.setBounds(50, 90, 75, 20);
			text_reason = new JTextArea(" " + " " + notice.getNo_reason());
			text_reason.setFont(new Font("宋体", Font.PLAIN, 14));
			text_reason.setBounds(50, 115, 440, 200);
			//设置自动换行
			text_reason.setLineWrap(true);
			//text_brief.setBackground(Color.gray);
			//禁止编辑
			text_reason.setEditable(false);
			contentPane.add(reason);
			contentPane.add(text_reason);

			//发布时间
			JLabel time = new JLabel("发布时间:");
			time.setFont(font);
			time.setBounds(300, 30, 75, 20);
			text_time = new JTextField(notice.getNo_time());
			text_time.setFont(font);
			text_time.setBounds(380, 30, 100, 20);
			//禁止编辑
			text_time.setEditable(false);
			contentPane.add(time);
			contentPane.add(text_time);
		}

	}

}
